﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Win32;
using System.Reflection;
using Winforms = System.Windows.Forms;

namespace WebReader
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        FileStream fsRead;
        FileStream fsWrite;
        XmlSerializer xs = new XmlSerializer(typeof(Settings));
       
        Settings s = new Settings();
        HabrWindow h;
        F1Window f;
        IXBTWindow i;

        RegistryKey myKey;
        Winforms.NotifyIcon ic;
        string filePath = "";

        public MainWindow()
        {

            InitializeComponent();

            filePath = Assembly.GetExecutingAssembly().Location.ToString();
            int num = filePath.LastIndexOf("\\");
            filePath = filePath.Substring(0, num);
            ic = new Winforms.NotifyIcon();
            ic.DoubleClick += _ICON_DoubleClick;
            ic.MouseDown += _ICON_MouseDown;
            ic.BalloonTipClicked += _iconTipClicked;
            ic.Icon = new System.Drawing.Icon(filePath + "\\icTray.ico");
            ic.BalloonTipTitle = " WebReader";
            ic.BalloonTipText = "WebReader";

            // Settings st = new Settings();
            myKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\", true);



            if (myKey.GetValue("WR") != null)
            {
                chbAutoRun.IsChecked = true;
            }


            fsRead = new FileStream(filePath + "\\Settings.xml", FileMode.Open, FileAccess.Read);
            s = (Settings)xs.Deserialize(fsRead);
            fsRead.Flush();
            fsRead.Close();
            s.CoordChanched += new EventHandler(SerialCoord);

            f = new F1Window(ref s, filePath);
            h = new HabrWindow(ref s, filePath);
            i = new IXBTWindow(ref s, filePath);
            LoadWindows();
            this.WindowState = WindowState.Minimized;
            this.Hide();
            ShowIcon();


        }


        public void ReLocateWindows()
        {
            fsRead = new FileStream(filePath + "\\Settings.xml", FileMode.Open, FileAccess.Read);
            s = (Settings)xs.Deserialize(fsRead);
            fsRead.Flush();
            fsRead.Close();
            s.CoordChanched += new EventHandler(SerialCoord);


            if (s.actHAbr == 1)
            {
               
                h.Left = s.habrSetx;
                h.Top = s.habrSety;
               
            }
            if (s.actIX == 1)
            {
                
                i.Left = s.ixSetx;
                i.Top = s.ixSety;
               
            }
            if (s.actF1 == 1)
            {
               
                f.Left = s.f1Setx;
                f.Top = s.f1Sety;
               
            }


        }
          
        public void LoadWindows()
        {

            if (s.actHAbr == 1)
            {
                h.Show();
                h.Left = s.habrSetx;
                h.Top = s.habrSety;
                h.ShowInTaskbar = false;
                chbHAbr.IsChecked = true;
            }
            if (s.actIX == 1)
            {
                i.Show();
                i.Left = s.ixSetx;
                i.Top = s.ixSety;
                i.ShowInTaskbar = false;
                chbIXBT.IsChecked = true;
            }
            if (s.actF1 == 1)
            {
                f.Show();
                f.Left = s.f1Setx;
                f.Top = s.f1Sety;
                f.ShowInTaskbar = false;
                chbF1.IsChecked = true;
            }
            

        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Serial();
            if (MessageBox.Show("Вы действительно хотите закрыть программу?", "Закрытие программы WebReader", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                h.Close();
                f.Close();
                i.Close();
            }
            else {
                e.Cancel = true;                
            }


           
            
        }
        public void SerialCoord(Object myObject, EventArgs myEventArgs)
        {
            Serial();
        }
        public void Serial()
        {


            fsWrite = new FileStream(filePath + "\\Settings.xml", FileMode.Create, FileAccess.ReadWrite);
                xs.Serialize(fsWrite, s);               
                fsWrite.Flush();
                fsWrite.Close();
                
           
        }
        private void chb_Click(object sender, RoutedEventArgs e)
        {
            CheckBox ch = (CheckBox)sender;
            if (ch.IsChecked == true)
            {
                switch (ch.Name){
                    case "chbHAbr" :
                        h = new HabrWindow(ref s, filePath);
                        h.Show();
                        s.actHAbr = 1;
                        h.Left = s.habrSetx;
                        h.Top = s.habrSety;
                        h.ShowInTaskbar = false;
                           break;
                    case "chbF1":
                           f = new F1Window(ref s, filePath);
                        f.Show();
                        s.actF1 = 1;
                        f.Left = s.f1Setx;
                        f.Top = s.f1Sety;
                        f.ShowInTaskbar = false;
                        break;
                    case "chbIXBT":
                        i = new IXBTWindow(ref s, filePath);
                        i.Show();
                        s.actIX = 1;
                        i.Left = s.ixSetx;
                        i.Top = s.ixSety;
                        i.ShowInTaskbar = false;
                           break;
                    case "chbAutoRun":
                           string str = Assembly.GetExecutingAssembly().Location.ToString();
                           myKey.SetValue("WR",str);
                           break;
                }
            }
            else
            {
                switch (ch.Name)
                {
                    case "chbHAbr":
                        h.Close();
                        s.actHAbr = 0;
                        break;
                    case "chbF1":
                        f.Close();
                        s.actF1 = 0;
                        break;
                    case "chbIXBT":
                        i.Close();
                        s.actIX = 0;
                        break;
                    case "chbAutoRun":
                        try
                        {
                            myKey.DeleteValue("WR");
                        }
                        catch { }
                        
                        break;
                }
            }
        }

        private void Window_Deactivated(object sender, EventArgs e)
        {
            ShowIcon();
        }

        private void ShowIcon()
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                ic.Visible = true;
                ic.ShowBalloonTip(2000);
            }
        }
        private void _iconTipClicked(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.WindowState = WindowState.Normal;
                this.Show();
                this.Activate();
                this.ShowInTaskbar = true;
                ic.Visible = false;
            }
        }
        private void _ICON_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.WindowState = WindowState.Normal;
                this.Show();
                this.Activate();
                this.ShowInTaskbar = true;
                ic.Visible = false;
            }
        }
        private void _ICON_MouseDown(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                ic.ShowBalloonTip(2000);
            }
        }

       
    }
}
